﻿using CompanyManageSystem.DLL;
using CompanyManageSystem.Model;
using CompanyManageSystem.Service;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CompanyManageSystem.UI.Repast
{
    public partial class Frm_SupplierFinance : BaseXtraForm
    {
        /// <summary>
        /// 构造方法
        /// </summary>
        public Frm_SupplierFinance()
        {
            InitializeComponent();

            this.BindCheckEdit(gridView1,false,true);
            this.BindRowIndicator(gridView1);
            this.SetGridViewStyle(gridView1,false,false);
        }

        /// <summary>
        /// 窗体加载完成
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Frm_SupplierFinance_Load(object sender, EventArgs e)
        {
            RepastService rservice = new RepastService();
            var supplierList = rservice.GetAllSuppliers();
            //插入所有供应商选项
            supplierList.Insert(0, new Supplier { ID = Guid.Empty, Name = "所有供应商" });
            SupplierEdit.EditValue = supplierList.Select(p => p.ID).ToList()[0];
            SupplierEdit.Properties.DataSource = supplierList;

            InitData();          
        }

        /// <summary>
        /// 初始化数据
        /// </summary>
        public void InitData()
        {
            RepastService rservice = new RepastService();
            var date1 = this.yearMonthsEdit1.DateTime;
            var date2 = this.yearMonthsEdit2.DateTime;
            Guid? supplierId = null;

            supplierId = (Guid)SupplierEdit.EditValue;

            gridControl1.DataSource = rservice.GetSupplierFinanceByFactor(date1,date2,supplierId);
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Frm_AddSupplierFinance frm = new Frm_AddSupplierFinance(status:EStatus.Add);
            frm.SaveEvent += SupplierFinance_AddSaveEvent;
            frm.ShowDialog();
        }
        private void SupplierFinance_AddSaveEvent(Object sender,EventArgs e)
        {
            InitData();
        }

        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem2_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {

        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem3_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            RepastService rservice = new RepastService();
            var finance = this.GetChecked<SupplierFinance>(gridView1);
            if (finance.Count == 0)
            {
                ShowInfo("请勾选需要删除的供应商付款信息!");
                return;
            }

            var financeId = finance.Select(p => p.ID).ToArray();
            if (ShowQuestion($"是否删除{finance.Count}条数据?"))
            {

            }
        }

        /// <summary>
        /// 查找
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void simpleButton1_Click(object sender, EventArgs e)
        {
            InitData();
        }
    }
}
